
package com.faxsun.web.controller.product;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.faxsun.core.pojos.enums.TagType;
import com.faxsun.core.service.TagService;

import mblog.core.pojos.Tag;
import mblog.web.controller.BaseController;
import mtons.modules.pojos.Page;

@Controller
@RequestMapping("/product-json")
public class ProductJsonController extends BaseController {

	@SuppressWarnings("unused")
	private static final Logger LOG = LoggerFactory
			.getLogger(ProductJsonController.class);

	@Autowired
	protected TagService tagService;

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public List<Tag> list(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "pagNo", required = false) Integer pageNo,
			@RequestParam(value = "max", required = false) Integer maxResults,
			ModelMap model) {

		pageNo = pageNo == null ? 1 : pageNo;
		// FIXME:常量9放入到数据库中
		Page page = wrapPage(pageNo);
		page.setMaxResults(maxResults == null ? 18 : maxResults);
		tagService.listTags(TagType.PRODUCT, page);

		// 把list转换为list<list>,且每个子list中仅有三个元素
		@SuppressWarnings("unchecked")
		List<Tag> tags = (List<Tag>) page.getResults();
		int maxPageId = ( page.getTotalCount() / 18)+1;
		if (pageNo > maxPageId) {
			 return Collections.emptyList();
		}
		else{
			return tags;
		}
		
	}
	
	
	
	
}

